package com.irdstudio.tdp.console.utils.excel;

import com.irdstudio.tdp.console.service.vo.ExcelExp;
import org.apache.poi.hssf.usermodel.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author lwc
 * @Description TODO
 * @createTime 2021年01月05日
 */
public class ExcelUtil {
    /**
     * @Description: 可生成单个、多个sheet
     * @param fileName 文件名称
     * @param mysheets sheets Name
     */
    public static void exportManySheetExcel(String fileName, List<ExcelExp> mysheets, HttpServletResponse response){

        //创建工作薄
        HSSFWorkbook wb = new HSSFWorkbook();
        List<ExcelExp> sheets = mysheets;
        //表头样式
        HSSFCellStyle style = wb.createCellStyle();
        // 创建一个居中格式
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        //字体样式
        HSSFFont fontStyle = wb.createFont();
        fontStyle.setFontName("微软雅黑");
        fontStyle.setFontHeightInPoints((short)12);
        fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        style.setFont(fontStyle);

        for(ExcelExp excel: sheets){
            //新建一个sheet
            //获取该sheet名称
            HSSFSheet sheet = wb.createSheet(excel.getFileName());
            //获取sheet的标题名
            String[] handers = excel.getHanders();
            //第一个sheet的第一行为标题
            HSSFRow rowFirst = sheet.createRow(0);
            //写标题
            for(int i=0;i<handers.length;i++){
                //获取第一行的每个单元格
                HSSFCell cell = rowFirst.createCell(i);
                //往单元格里写数据
                cell.setCellValue(handers[i]);
                //加样式
                cell.setCellStyle(style);
                //设置每列的列宽
                sheet.setColumnWidth(i, 4000);
            }

            //写数据集
            List<String[]> dataset = excel.getDataset();
            for(int i=0;i<dataset.size();i++){
                //获取该对象
                String[] data = dataset.get(i);

                //创建数据行
                HSSFRow row = sheet.createRow(i+1);

                for(int j=0;j<data.length;j++){
                    //设置对应单元格的值
                    row.createCell(j).setCellValue(data[j]);
                }
            }
        }
        OutputStream outputStream = null;
        // 写文件
        try {
            outputStream = response.getOutputStream();
            setResponseHeader(response,fileName);
            wb.write(outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    /**
     * 向客户端发送响应流方法
     * @param response
     * @param fileName
     */
    public static void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}
